<html>
<head><meta charset="utf-8"><title>References to (un)init · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References.20to.20(un)init.html">References to (un)init</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="196591214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References%20to%20%28un%29init/near/196591214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References.20to.20(un)init.html#196591214">(May 06 2020 at 06:12)</a>:</h4>
<p>Hi all. Is <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/77" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/77">https://github.com/rust-lang/unsafe-code-guidelines/issues/77</a> the right issue for the question of whether <code>&amp;[u8]</code> pointing to uninitialized bytes should be UB?</p>



<a name="196591283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References%20to%20%28un%29init/near/196591283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References.20to.20(un)init.html#196591283">(May 06 2020 at 06:14)</a>:</h4>
<p>The context is: I’m wondering whether new memory-allocation methods should return <code>NonNull&lt;MaybeUninit&lt;u8&gt;&gt;</code> instead of <code>NonNull&lt;u8&gt;</code></p>



<a name="196591515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References%20to%20%28un%29init/near/196591515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References.20to.20(un)init.html#196591515">(May 06 2020 at 06:16)</a>:</h4>
<p>raw pointers and NonNull have lesser assurances than references do, so NonNull&lt;u8&gt; is fine to point to Uninit</p>



<a name="196591707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References%20to%20%28un%29init/near/196591707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References.20to.20(un)init.html#196591707">(May 06 2020 at 06:21)</a>:</h4>
<p>I understand that. The point of <code>MaybeUninit</code> would be to encourage patterns such as dereferencing to <code>&amp;mut [MaybeUninit&lt;_&gt;]</code> for initialization</p>



<a name="196650323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References%20to%20%28un%29init/near/196650323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References.20to.20(un)init.html#196650323">(May 06 2020 at 15:21)</a>:</h4>
<p>I suppose.</p>
<p>I don't think that's a very easy to use pattern compared to just writing to a pointer.</p>



<a name="196686894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References%20to%20%28un%29init/near/196686894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References.20to.20(un)init.html#196686894">(May 06 2020 at 19:44)</a>:</h4>
<p><span class="user-mention" data-user-id="219747">@Simon Sapin</span> both the one you linked and <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/71" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/71">https://github.com/rust-lang/unsafe-code-guidelines/issues/71</a> are related</p>



<a name="196686945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References%20to%20%28un%29init/near/196686945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References.20to.20(un)init.html#196686945">(May 06 2020 at 19:45)</a>:</h4>
<p>basically, if both the reference and the int type are very strict in their validity invariant, then having such a slice is UB</p>



<a name="196686955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References%20to%20%28un%29init/near/196686955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References.20to.20(un)init.html#196686955">(May 06 2020 at 19:45)</a>:</h4>
<p>if <em>either</em> one is relaxed, it's fine</p>



<a name="196687052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References%20to%20%28un%29init/near/196687052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References.20to.20(un)init.html#196687052">(May 06 2020 at 19:45)</a>:</h4>
<p>I personally feel we should be relaxed with references, and I think my last proposal for handling <code>&amp;!</code> was generally received well, but I am not sure if there would be consensus for the plan I raised there</p>



<a name="196687186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References%20to%20%28un%29init/near/196687186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References.20to.20(un)init.html#196687186">(May 06 2020 at 19:46)</a>:</h4>
<p>as for integers, I keep flip-flopping, and right now my inclination is to indeed make them UB when uninitialized -- but I know that certainly has no consensus ATM</p>



<a name="197506352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References%20to%20%28un%29init/near/197506352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/References.20to.20(un)init.html#197506352">(May 14 2020 at 04:59)</a>:</h4>
<p>I like that thought direction, <span class="user-mention" data-user-id="219747">@Simon Sapin</span>.  Could lead to things like <code>&amp;mut [MaybeUninit&lt;T&gt;]</code>s out of <code>Vec&lt;T&gt;</code>s too, to help avoid accidental write-outside-allocated-space issues that are so easy when just using <code>as_mut_ptr</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>